package com.kust.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.kust.commomUtils.Result;
import com.kust.serviceBase.exceptionHandler.GuliException;
import com.kust.vod.service.VodService;
import com.kust.vod.utils.ConstantVodUtils;
import com.kust.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 马强
 * @Description
 * @create 2022-06-28 10:17
 */
@RestController
@RequestMapping("/eduVod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("/upLoadAliyunVideo")
    public Result upLoadAliyunVideo(MultipartFile file){
        String videoId=vodService.uploadVideoALY(file);
        return Result.ok().data("videoId",videoId);
    }


    //根据视频id删除视频
    @DeleteMapping("removeALYVide/{id}")
    public Result removeALYVide(@PathVariable("id") String id){
        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //删除视频request对象
            DeleteVideoRequest request=new DeleteVideoRequest();
            //向request设置id
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return Result.ok();
        }catch (Exception e){
                e.printStackTrace();
                throw new GuliException(20001,"删除失败");
        }

    }

    //删除多个阿里云视频
    @DeleteMapping("/delete-batch")
    public Result deleteBatch(@RequestParam("videoList") List<String> list){
        vodService.removeMoreALYvideo(list);
        return Result.ok();
    }

    //根据视频id获取凭证
    @GetMapping("getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable("id") String id){
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return Result.ok().data("playAuth",playAuth);
        }catch(Exception e) {
            throw new GuliException(20001,"获取凭证失败");
        }
    }
}
